package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ExtCproductServiceImpl implements ExtCproductService {

    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;


    @Override
    public void save(ExtCproduct extCproduct) {
//1.设置id    在Controller中已完成
//2.计算附件的小计金额
//3.设置附件小计金额
//4.保存附件
//5.根据id查询购销合同
//6.设置购销合同的总金额
//7.设置附件数量
//8.更新合同


//        对合同的影响 附件数量+1   总金额+当前附件的小计金额
        double amount = extCproduct.getCnumber() * extCproduct.getPrice();
        extCproduct.setAmount(amount);

        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setExtNum(contract.getExtNum()+1);
        contract.setTotalAmount(contract.getTotalAmount()+amount);
        contractDao.updateByPrimaryKeySelective(contract);

        extCproductDao.insertSelective(extCproduct);
    }

    @Override
    public void update(ExtCproduct extCproduct) {
//1.计算修改之后的小计金额
//2.获取修改之前的小计金额
//3.设置附件的新的小计金额
//4.更新附件
//5.查询购销合同
//6.设置合同总金额
//7.更新购销合同

//        对合同的影响 ：合同的总金额=原总金额-附件旧的小计金额+附件新的小计金额
        double amount_new = extCproduct.getCnumber() * extCproduct.getPrice();
        extCproduct.setAmount(amount_new);
//        获取旧的小计金额
        ExtCproduct extCproductOld = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        Double amount_old = extCproductOld.getAmount();

        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount()-amount_old+amount_new);
        contractDao.updateByPrimaryKeySelective(contract);

        extCproductDao.updateByPrimaryKeySelective(extCproduct);
    }

    @Override
    public void delete(String id) {
//1.根据id查询附件
//2.获取删除附件的小计金额
//3.查询购销合同
//4.设置购销合同总金额
//5.设置购销合同的附件数
//6.更新合同
//7.删除附件


//        对合同影响： 1、附件数-1  2、总金额-当前附件的小计金额
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        Double amount = extCproduct.getAmount();

        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setExtNum(contract.getExtNum()-1);
        contract.setTotalAmount(contract.getTotalAmount()-amount);
        contractDao.updateByPrimaryKeySelective(contract);

        extCproductDao.deleteByPrimaryKey(id);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ExtCproduct> list = extCproductDao.selectByExample(example);
        return new PageInfo(list,5);
    }
}
